﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Dapper.SimpleDao.Factory
{
    /// <summary>
    /// 字段排序泛型类
    /// </summary>
    /// <typeparam name="T">数据实体</typeparam>
    public class PropertySort<T> where T : class
    {
        /// <summary>
        /// 字段名
        /// </summary>
        public string PropertyName { get; set; }
        /// <summary>
        /// 是否正序
        /// </summary>
        public bool Ascending { get; set; }
    }
    /// <summary>
    /// 排序辅助类
    /// </summary>
    public static class PropertySortHelper
    {
        /// <summary>
        /// 字段排序Lambda转换
        /// </summary>
        /// <typeparam name="T">数据实体</typeparam>
        /// <param name="expression">表达式</param>
        /// <param name="ascending">是否正序</param>
        /// <returns></returns>
        public static PropertySort<T> Sort<T>(Expression<Func<T, object>> expression, bool ascending = true) where T : class
        {
            PropertyInfo propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
            return new PropertySort<T>
            {
                PropertyName = propertyInfo.Name,
                Ascending = ascending
            };
        }
    }
}
